package com.inei.cenacom.view;

import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Spinner;

import com.inei.cenacom.R;
import com.inei.cenacom.controller.Cap100Controller;
import com.inei.cenacom.controller.CaratulaController;
import com.inei.cenacom.model.entity.Cap100;
import com.inei.cenacom.model.entity.Caratula;
import com.inei.cenacom.tools.Mensaje;
import com.inei.cenacom.tools.OnSiguienteListener;
import com.inei.cenacom.tools.Validacion;

public class Caratula_5_Fragment extends Fragment {

	private EditText etC6_1_1, etC6_1_2, etC6_2_1, etC6_2_2, etC6_3_1,
			etC6_3_2, etC7_1_1, etC7_2_1, etC7_1_3, etC7_2_3, etC7_1_4,
			etC7_2_4, etC7_1_5, etC7_2_5, etC7_1_6, etC7_2_6;
	private Spinner etC7_1_2, etC7_2_2;
	private CheckBox cbC7_1_4, cbC7_1_5, cbC7_1_6, cbC7_2_4, cbC7_2_5,
			cbC7_2_6;
	private CaratulaController cCon;
	private Caratula c;
	private Cap100 cap;
	private Cap100Controller capCon;
	private Validacion v;
	private Mensaje m;
	private Button bSiguiente5;
	private OnSiguienteListener onSiguienteListener;

	public OnSiguienteListener getOnSiguienteListener() {
		return onSiguienteListener;
	}

	public void setOnSiguienteListener(OnSiguienteListener onSiguienteListener) {
		this.onSiguienteListener = onSiguienteListener;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fragment_caratula_5, container, false);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onViewCreated(view, savedInstanceState);
		iniciarComponentes(view);
		// agregarEventos();
		cargarDatos();
	}

	private void agregarEventos() {
		cbC7_1_4.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					etC7_1_4.setEnabled(true);
				} else {
					etC7_1_4.setEnabled(false);
					etC7_1_4.setText("");
				}
			}
		});

		cbC7_1_5.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					etC7_1_5.setEnabled(true);
				} else {
					etC7_1_5.setEnabled(false);
					etC7_1_5.setText("");
				}
			}
		});

		cbC7_1_6.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					etC7_1_6.setEnabled(true);
				} else {
					etC7_1_6.setEnabled(false);
					etC7_1_6.setText("");
				}
			}
		});

		cbC7_2_4.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					etC7_2_4.setEnabled(true);
				} else {
					etC7_2_4.setEnabled(false);
					etC7_2_4.setText("");
				}
			}
		});

		cbC7_2_5.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					etC7_2_5.setEnabled(true);
				} else {
					etC7_2_5.setEnabled(false);
					etC7_2_5.setText("");
				}
			}
		});

		cbC7_2_6.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					etC7_2_6.setEnabled(true);
				} else {
					etC7_2_6.setEnabled(false);
					etC7_2_6.setText("");
				}
			}
		});

	}

	private void cargarDatos() {
		Bundle b = getArguments();
		c = new Caratula();
		c.setID_N(b.getString("ID_N"));
		c.setANIO(b.getInt("ANIO"));
		c = cCon.buscar(c);
		if (c != null) {
			etC6_1_1.setText(c.getVI1A());
			etC6_1_2.setText(c.getVI1B());
			etC6_2_1.setText(c.getVI2A());
			etC6_2_2.setText(c.getVI2B());
			etC6_3_1.setText(c.getVI3A());
			etC6_3_2.setText(c.getVI3B());

			etC7_1_1.setText(c.getVII1A());
			if (c.getVII1B() != null) {
				if (c.getVII1B().length() != 0) {
					etC7_1_2.setSelection(Integer.parseInt(c.getVII1B()));
				}
			}
			etC7_1_3.setText(c.getVII1C());
			etC7_1_4.setText(c.getVII1D() );
			etC7_1_5.setText(c.getVII1E());
			etC7_1_6.setText(c.getVII1F());

			etC7_2_1.setText(c.getVII2A());
			if (c.getVII2B() != null) {
				if (c.getVII2B().length() != 0) {
					etC7_2_2.setSelection(Integer.parseInt(c.getVII2B()));
				}
			}
			etC7_2_3.setText(c.getVII2C());
			etC7_2_4.setText(c.getVII2D());
			etC7_2_5.setText(c.getVII2E());
			etC7_2_6.setText(c.getVII2F());
		}
	}

	private void iniciarComponentes(View view) {
		m = new Mensaje(getActivity());
		v = new Validacion(m);
		capCon = new Cap100Controller(getActivity());
		cCon = new CaratulaController(getActivity());

		TextView tvCAPVI = (TextView)getView().findViewById(R.id.tvCAPVI);
		TextView tvCAPVII = (TextView)getView().findViewById(R.id.tvCAPVII);
		int anio = getArguments().getInt("ANIO");
		if(anio==2011){
			tvCAPVI.setBackgroundColor(getActivity().getResources().getColor(R.color.Azul));
			tvCAPVI.setTextColor(Color.WHITE);
			tvCAPVII.setBackgroundColor(getActivity().getResources().getColor(R.color.Azul));
			tvCAPVII.setTextColor(Color.WHITE);
		}
		bSiguiente5 = (Button) getView().findViewById(R.id.bSiguiente5);
		bSiguiente5.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(onSiguienteListener!=null){
					onSiguienteListener.OnSiguiente(5);
				}
			}
		});
		
		etC6_1_1 = (EditText) view.findViewById(R.id.etC6_1_1);
		etC6_1_2 = (EditText) view.findViewById(R.id.etC6_1_2);
		etC6_2_1 = (EditText) view.findViewById(R.id.etC6_2_1);
		etC6_2_2 = (EditText) view.findViewById(R.id.etC6_2_2);
		etC6_3_1 = (EditText) view.findViewById(R.id.etC6_3_1);
		etC6_3_2 = (EditText) view.findViewById(R.id.etC6_3_2);

		etC7_1_1 = (EditText) view.findViewById(R.id.etC7_1_1);
		etC7_1_2 = (Spinner) view.findViewById(R.id.etC7_1_2);

		etC7_1_3 = (EditText) view.findViewById(R.id.etC7_1_3);
		etC7_1_3.setEnabled(false);
		etC7_1_4 = (EditText) view.findViewById(R.id.etC7_1_4);
		etC7_1_5 = (EditText) view.findViewById(R.id.etC7_1_5);
		etC7_1_6 = (EditText) view.findViewById(R.id.etC7_1_6);
		// cbC7_1_4 = (CheckBox) view.findViewById(R.id.cbC7_1_4);
		// cbC7_1_5 = (CheckBox) view.findViewById(R.id.cbC7_1_5);
		// cbC7_1_6 = (CheckBox) view.findViewById(R.id.cbC7_1_6);
		etC7_2_1 = (EditText) view.findViewById(R.id.etC7_2_1);
		etC7_2_2 = (Spinner) view.findViewById(R.id.etC7_2_2);
		etC7_2_3 = (EditText) view.findViewById(R.id.etC7_2_3);
		etC7_2_4 = (EditText) view.findViewById(R.id.etC7_2_4);
		etC7_2_5 = (EditText) view.findViewById(R.id.etC7_2_5);
		etC7_2_6 = (EditText) view.findViewById(R.id.etC7_2_6);
		// cbC7_2_4 = (CheckBox) view.findViewById(R.id.cbC7_2_4);
		// cbC7_2_5 = (CheckBox) view.findViewById(R.id.cbC7_2_5);
		// cbC7_2_6 = (CheckBox) view.findViewById(R.id.cbC7_2_6);
		etC7_1_2.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				if (pos != 0) {
					etC7_1_3.setEnabled(true);
				} else {
					etC7_1_3.setEnabled(false);
					etC7_1_3.setText("");
				}

			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
		etC7_2_2.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				if (pos != 0) {
					etC7_2_3.setEnabled(true);
				} else {
					etC7_2_3.setEnabled(false);
					etC7_2_3.setText("");
				}

			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
	}

	public Caratula guardar() throws Exception {
		if (!v.validarDNI(etC6_1_1, true)) {
			throw new Exception();
		} else if (!v.validarTextoA(etC6_1_2, true)) {
			throw new Exception();
		} else if (!v.validarDNI(etC6_2_1, false)) {
			throw new Exception();
		} else if (!v.validarTextoA(etC6_2_2, false)) {
			throw new Exception();
		} else if (!v.validarDNI(etC6_3_1, false)) {
			throw new Exception();
		} else if (!v.validarTextoA(etC6_3_2, false)) {
			throw new Exception();
		} else if (!v.validarTextoA(etC7_1_1, true)) {
			throw new Exception();
		} else if (!v.validarGrado(etC7_1_2, true, etC7_1_3)) {
			throw new Exception();
		} else if (!v.validarTelefono(etC7_1_4, true)) {
			throw new Exception();
		} else if (!v.validarTelefono(etC7_1_5, true)) {
			throw new Exception();
		} else if (!v.validarTelefono(etC7_1_6, true)) {
			throw new Exception();
		} else if (!v.validarTextoA(etC7_2_1, true)) {
			throw new Exception();
		} else if (!v.validarGrado(etC7_2_2, true, etC7_2_3)) {
			throw new Exception();
		} else if (!v.validarTelefono(etC7_2_4, true)) {
			throw new Exception();
		} else if (!v.validarTelefono(etC7_2_5, true)) {
			throw new Exception();
		} else if (!v.validarTelefono(etC7_2_6, true)) {
			throw new Exception();
		} else {
			c.setUSR_EDICION(getArguments().getString("USUARIO"));
			c.setVI1A(etC6_1_1.getText().toString());
			c.setVI1B(etC6_1_2.getText().toString());
			c.setVI2A(etC6_2_1.getText().toString());
			c.setVI2B(etC6_2_2.getText().toString());
			c.setVI3A(etC6_3_1.getText().toString());
			c.setVI3B(etC6_3_2.getText().toString());

			c.setVII1A(etC7_1_1.getText().toString());
			c.setVII1B("" + etC7_1_2.getSelectedItemPosition());
			c.setVII1C(etC7_1_3.getText().toString());
			c.setVII1D(etC7_1_4.getText().toString());
			c.setVII1E(etC7_1_5.getText().toString());
			c.setVII1F(etC7_1_6.getText().toString());

			c.setVII2A(etC7_2_1.getText().toString());
			c.setVII2B("" + etC7_2_2.getSelectedItemPosition());
			c.setVII2C(etC7_2_3.getText().toString());
			c.setVII2D(etC7_2_4.getText().toString());
			c.setVII2E(etC7_2_5.getText().toString());
			c.setVII2F(etC7_2_6.getText().toString());
			return c;
		}
	}

	public int obtenerValor(EditText et) {
		if (et.getText().toString().length() == 0) {
			return 0;
		} else {
			return Integer.parseInt(et.getText().toString());
		}
	}
}
